﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;

namespace SalesManagement.Controls.List
{
    public partial class InvoiceTypeForm : System.Web.UI.UserControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show()
        {
            this.winDetails.Show();
        }
        public void Show(string invoiceTypeId)
        {
            InvoiceTypeEntity invoiceTypeE = new InvoiceTypeEntity();
            InvoiceTypeService invoiceTypeS = InvoiceTypeService.GetInstance();
            invoiceTypeE.InvoiceTypeId = int.Parse(invoiceTypeId);
            invoiceTypeE = invoiceTypeS.LoadByPrimaryKey(invoiceTypeE);
            if (null == invoiceTypeE)
                return;
            hdfInvoiceTypeId.Value = invoiceTypeE.InvoiceTypeId;
            txtInvoiceTypeName.Text = invoiceTypeE.InvoiceTypeName;
            txtDescription.Text = invoiceTypeE.Description;
            ckbIsActive.Checked = invoiceTypeE.IsActive;
            this.winDetails.Show();
        }
        public void ShowAndReset()
        {
            this.FormPanel1.Reset();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            InvoiceTypeEntity invoiceTypeE = new InvoiceTypeEntity();
            InvoiceTypeService invoiceTypeS = InvoiceTypeService.GetInstance();
            if ("" != hdfInvoiceTypeId.Value.ToString())
            {
                string invoiceTypeId = hdfInvoiceTypeId.Value.ToString();
                invoiceTypeE.InvoiceTypeId = int.Parse(invoiceTypeId);
                invoiceTypeE = invoiceTypeS.LoadByPrimaryKey(invoiceTypeE);
                invoiceTypeE.InvoiceTypeName = txtInvoiceTypeName.Text;
                invoiceTypeE.Description = txtDescription.Text;
                invoiceTypeE.IsActive = ckbIsActive.Checked;
                invoiceTypeE.UpdatedDate = DateTime.Now;
                invoiceTypeE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                invoiceTypeS.Update(invoiceTypeE);
            }
            else
            {
                invoiceTypeE.InvoiceTypeName = txtInvoiceTypeName.Text;
                invoiceTypeE.Description = txtDescription.Text;
                invoiceTypeE.IsActive = ckbIsActive.Checked;
                invoiceTypeE.CreatedDate = DateTime.Now;
                invoiceTypeE.CreatedBy = FSSContext.Current.User.EmployeeId;
                invoiceTypeE.UpdatedDate = DateTime.Now;
                invoiceTypeE.UpdatedBy = FSSContext.Current.User.EmployeeId;
                invoiceTypeS.Insert(invoiceTypeE);
            }
            this.winDetails.Hide();
            OnSaveClick();
        }
    }
}